import NodeRSA from "node-rsa";
import { config } from "../config";
import fs from "fs";
import path from "path";
 class rsa {
  //私钥
  private: any;
  //公钥
  public:any;
  constructor() {
  this.private = this.readfile(path.join(config.initPlugins.staticPath,'/rsa/rsa_private_key.pem'))
  this.public = this.readfile(path.join(config.initPlugins.staticPath,'/rsa/rsa_public_key.pem'))
  }
  //用私钥解密
  public async decryptPrivate(text: string): Promise<string> {
    let prikey:any =new NodeRSA(this.private);
    prikey.setOptions({encryptionScheme: 'pkcs1'});
    return await prikey.decrypt(text,"UTF-8");
  }
  //用公钥加密 
  public async encryptPublic(text: string): Promise<string> {
    let pubkey:any =new NodeRSA(this.public);
    pubkey.setOptions({encryptionScheme: 'pkcs1'});
    return await pubkey.encrypt(text,'base64');
  }
  
  //封装读写文件
  public readfile(path: string): any {
    return fs.readFileSync(path).toString("utf-8");
  }
}
export default new rsa()
